﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OPLWeb.Models;

namespace OPLWeb.Controllers
{
    public class HomeController : Controller
    {
        OPLWeb.Models.ORITAEntities db = new Models.ORITAEntities();
        public ActionResult Index()
        {
            HomeWrapper wrapper = new HomeWrapper();
            ViewBag.CurrentSeason = db.Seasons.Max(p => p.ID);
            wrapper.LeagueTable = db.Teams.OrderByDescending(p => p.Points).Take(4).ToList();
            wrapper.News = db.NewsUpdates.Where(p => p.IsNews).OrderByDescending(p => p.DateTime).Take(3).ToList();
            wrapper.Updates = db.NewsUpdates.Where(p => !p.IsNews).OrderByDescending(p => p.DateTime).Take(5).ToList();
            wrapper.Updates.AddRange(db.NewsUpdates.Where(p => p.IsNews).OrderByDescending(p => p.DateTime).Take((5 - wrapper.Updates.Count)).ToList());
            wrapper.MatchSummary = db.Fixtures.Where(p => p.Date < DateTime.Now && p.ManOfTheMatch != null).OrderByDescending(p => p.Date).Take(2).ToList();
            wrapper.UpcomingFixtures = db.Fixtures.Where(p => p.Date > DateTime.Now && p.ManOfTheMatch == null).OrderBy(p => p.Date).Take(2).ToList();
            return View(wrapper);
        }

        public ActionResult Voice()
        {

            return View();
        }
        public PartialViewResult GetNewsItem(int id)
        {
            NewsUpdate thisNews = db.NewsUpdates.Where(p => p.ID == id).FirstOrDefault();
            return PartialView("SlideImageContainer", thisNews);
        }


        public ActionResult Speech(string data)
        {
            ViewBag.CurrentSeason = db.Seasons.Max(p => p.ID);
            if (data.Contains("home"))
            {
                return Json("Home/Index/" + ViewBag.CurrentSeason, JsonRequestBehavior.AllowGet);
            }
            if (data.Contains("new"))
            {
                return Json("News/Index/" + ViewBag.CurrentSeason, JsonRequestBehavior.AllowGet);
            }
            if (data.Contains("match"))
            {
                return Json("Fixtures/Index/" + ViewBag.CurrentSeason, JsonRequestBehavior.AllowGet);
            }
            if (data.Contains("league"))
            {
                return Json("League/Index/" + ViewBag.CurrentSeason, JsonRequestBehavior.AllowGet);
            }
            return Json("error", JsonRequestBehavior.AllowGet);
        }
        public JsonResult Submit()
        {

            return Json("tst", JsonRequestBehavior.AllowGet);
        }
        public ActionResult Contact()
        {
            ViewBag.Message = "Your quintessential contact page.";

            return View();
        }
    }
}
